import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fyi/generated/i18n.dart';
import 'package:fyi/pages/splash_page.dart';
import 'package:fyi/router/routes.dart';
import 'package:fyi/utils/common_util.dart';

class RouteApp extends StatelessWidget{

  final Map<String,WidgetBuilder> routeMap;

  RouteApp(this.routeMap);

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,
      home:new Builder(builder: (context){
        return FreeLocalizations(
          key:freeLocalizationStateKey ,
          child: SplashPage(),
        );
      }),
      routes: routeMap,
    );
    //return wrapApp(SplashPage());
  }

}

Widget wrapApp(Widget page){
  return new MaterialApp(
    localizationsDelegates: const [
      S.delegate,
      GlobalMaterialLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate
    ],
    supportedLocales: S.delegate.supportedLocales,
    home:new Builder(builder: (context){
      return FreeLocalizations(
        key:freeLocalizationStateKey ,
        child: page,
      );
    }),
    routes: Routes.routeMap,
  );
}

Widget wrapApp2(Widget page){
  return FreeLocalizations(
    child: page,
  );
}

GlobalKey<_FreeLocalizations> freeLocalizationStateKey =
new GlobalKey<_FreeLocalizations>();

void changeLocal(Locale locale){
  freeLocalizationStateKey.currentState.changeLocale(locale);
}

class FreeLocalizations extends StatefulWidget {
  final Widget child;

  FreeLocalizations({Key key, this.child}) : super(key: key);

  @override
  State<FreeLocalizations> createState() {
    return new _FreeLocalizations();
  }
}

Locale _locale = CommonUtil.getSelectedLocale();

class _FreeLocalizations extends State<FreeLocalizations> {

  changeLocale(Locale locale) {
    setState(() {
      _locale = locale;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Localizations.override(
      context: context,
      locale: _locale,
      child: widget.child,
    );
  }
}